﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using YDModel;
using YDServices.Comm;

namespace YDServices
{
    public class OrderService : BaseService
    {
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="conds"></param>
        /// <returns></returns>
        public List<T_Order> getList(ConditionItem[] conds)
        { 
             try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    var list = db.T_Order.Where(o=>o.State!=false).AsQueryable();
                    if (conds != null)
                    {
                        var qb = QueryBuilder<T_Order>.Create().AddConditions(conds);
                        list = list.Where(qb.ToExpression());
                    }
                    return list.ToList();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
                return null;
            }
        }

        /// <summary>
        /// 查询视图list
        /// </summary>
        /// <param name="conds"></param>
        /// <returns></returns>
        public List<V_Order> getViewList(ConditionItem[] conds)
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    var list = db.V_Order.Where(o => o.State != false).AsQueryable();
                    if (conds != null)
                    {
                        var qb = QueryBuilder<V_Order>.Create().AddConditions(conds);
                        list = list.Where(qb.ToExpression());
                    }
                    return list.ToList();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
                return null;
            }
        }


        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="tu"></param>
        public void AddItem(T_Order tu)
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    using (DbContextTransaction transaction = db.Database.BeginTransaction())
                    {
                        tu.State = true;
                        tu.CreatedTime = GetSqlNow();
                        tu.CreatedBy = "[" + Global.loginUser.ID + "]" + Global.loginUser.UserName;
                        db.T_Order.Add(tu);
                        db.SaveChanges();
                        //插入数据到OrderReport
                        //T_Order_Report or = new T_Order_Report();
                        //or.Order_ID = tu.ID;
                        //or.OperatintTime = tu.OperatintTime;
                        //or.Factory_Name = tu.Factory_Name;
                        //or.Spec_Name = tu.Spec_Name;
                        //or.SerialNumber = tu.SerialNumber;
                        //or.Quantity = tu.Quantity;
                        //or.Weight = tu.Weight;
                        //or.SumWeight = tu.SumWeight;
                        //or.UseWeight = tu.UseWeight;
                        //or.SurplusWeight = tu.SurplusWeight;
                        //or.UnitPrice = tu.UnitPrice;
                        //or.TotalPrice = tu.TotalPrice;
                        //or.UseQuantity = tu.UseQuantity;
                        //or.SurplusQuantity = tu.SurplusQuantity;
                        //db.T_Order_Report.Add(or);

                        T_Order_Report_Link orl = new T_Order_Report_Link();
                        orl.Order_ID = tu.ID;
                        db.T_Order_Report_Link.Add(orl);
                        db.SaveChanges();

                        transaction.Commit();
                    }
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
            }
        }



        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="tu"></param>
        public void EditItem(T_Order tu)
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    //using (DbContextTransaction transaction = db.Database.BeginTransaction())
                    //{
                        var item = db.T_Order.Where(o => o.ID == tu.ID && o.State == true).FirstOrDefault();
                        if (item == null)
                        {
                            throw new ApplicationException("订单信息不存在!");
                        }
                        item.Factory_ID = tu.Factory_ID;
                        item.Factory_Name = tu.Factory_Name;
                        item.OperatintTime = tu.OperatintTime;
                        item.Quantity = tu.Quantity;
                        item.Weight = tu.Weight;
                        item.SumWeight = tu.SumWeight;
                        item.SerialNumber = tu.SerialNumber;
                        item.Spec_ID = tu.Spec_ID;
                        item.Spec_Name = tu.Spec_Name;
                        item.UnitPrice = tu.UnitPrice;
                        item.TotalPrice = tu.TotalPrice; 
                        item.ModifiedBy = "[" + Global.loginUser.ID + "]" + Global.loginUser.UserName;
                        item.ModifiedTime = GetSqlNow();

                        db.SaveChanges();


                        //更新数据到OrderReport 
                        //var report = db.T_Order_Report.Where(o => o.ID == tu.ID).FirstOrDefault();
                        //if(report!=null)
                        //{

                        //    report.Factory_Name = tu.Factory_Name;
                        //    report.OperatintTime = tu.OperatintTime;
                        //    report.Quantity = tu.Quantity;
                        //    report.Weight = tu.Weight;
                        //    report.SumWeight = tu.SumWeight;
                        //    report.SerialNumber = tu.SerialNumber; 
                        //    report.Spec_Name = tu.Spec_Name;
                        //    report.UnitPrice = tu.UnitPrice;
                        //    report.TotalPrice = tu.TotalPrice;
                        //}

                    //    transaction.Commit();
                    //}
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
            }
        }



        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="tu"></param>
        /// <param name="loginUser"></param>
        public void DelItem(T_Order item)  //List<T_Order> tuList 
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    using (DbContextTransaction transaction = db.Database.BeginTransaction())
                    {
                        var fact = db.T_Order.Where(o => o.ID == item.ID).FirstOrDefault();
                        if (fact == null)
                        {
                            throw new ApplicationException("订单信息不存在!");
                        }
                        fact.State = false;
                        fact.DeletedTime = GetSqlNow();
                        fact.DeletedBy = "[" + Global.loginUser.ID + "]" + Global.loginUser.UserName;

                        db.SaveChanges();
                        //同步删除OrderReport 中的数据
                        //var report = db.T_Order_Report.Where(o => o.Order_ID == item.ID).ToList();
                        //if (report != null)
                        //{
                        //    db.T_Order_Report.RemoveRange(report);
                        //} 
                        var report = db.T_Order_Report_Link.Where(o => o.Order_ID == item.ID).FirstOrDefault();
                        if (report != null)
                        {
                            report.State = false; 
                        }
                        db.SaveChanges();
                        transaction.Commit();
                    }

                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
            }
        }
 

    }
}
